package com.generation.builder;

import com.generation.Bean.Constants;
import com.generation.Bean.FieldInfo;
import com.generation.Bean.TableInfo;
import com.generation.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mockingjay
 * @date 2023/4/10 - 12:09
 */
public class BuildQuery {

    private static final Logger LOGGER = LoggerFactory.getLogger(BuildrPoJo.class);

    public static void excute(TableInfo tableInfo){
        File folder =new File(Constants.PATH_PARAM);
        if(!folder.exists()){
            folder.mkdirs();
        }

        File poFile=new File(folder,tableInfo.getBeanName()+StringUtils.processField(Constants.SUFFIX_BEAN_PARMA,true)+".java");

        OutputStream out=null;
        OutputStreamWriter ow=null;
        BufferedWriter bw=null;
        StringBuffer buffer=new StringBuffer();
        try {
            out=new FileOutputStream(poFile);
            ow=new OutputStreamWriter(out,"utf8");
            bw=new BufferedWriter(ow);
            bw.write("package "+Constants.PACKAGE_PARAM+";");
            bw.newLine();
            bw.newLine();
            if(tableInfo.getHavaDateTime()||tableInfo.getHaveDate()){
                bw.write("import java.util.Date;");
                bw.newLine();
            }
            if(tableInfo.getHaveBigDecimal()){
                bw.write("import java.math.BigDecimal;");
                bw.newLine();
            }
            bw.newLine();
            bw.newLine();
            //构筑类注释
            BuildComment.createClassComment(bw,tableInfo.getComment()+"查询对象");
            bw.write("public class "+tableInfo.getBeanName()+Constants.SUFFIX_BEAN_PARMA+" { ");
            bw.newLine();
            bw.newLine();
            //钩住类属性
            for(FieldInfo info:tableInfo.getFieldInfoList()){
                BuildComment.createFieldComment(bw,info.getComment());
                bw.newLine();
                bw.write("\tprivate "+info.getJavaType()+" "+info.getPropertyName()+";");
                bw.newLine();
                bw.newLine();

                //如果是String类型加上fuzzy
                if(ArrayUtils.contains(Constants.SQL_STRING_TYPE,info.getSqlType())){
                    String propertyName=info.getPropertyName()+Constants.SUFFIX_BEAN_QUERY_FUZZY;
                    bw.write("\tprivate "+info.getJavaType()+" "+propertyName+";");
                    bw.newLine();
                    bw.newLine();
                }
                //日期类型加上start和end
                if(ArrayUtils.contains(Constants.SQL_DATE_TYPE,info.getSqlType())||ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPE,info.getSqlType())){
                    String startDate=info.getPropertyName()+Constants.SUFFIX_BEAN_QUERY_TIME_START;
                    String endDate=info.getPropertyName()+Constants.SUFFIX_BEAN_QUERY_TIME_END;
                    bw.write("\tprivate String "+startDate+";");
                    bw.newLine();
                    bw.newLine();
                    bw.write("\tprivate String "+endDate+";");
                    bw.newLine();
                    bw.newLine();
                }
            }
            //构筑get和set方法
            buildGetSet(tableInfo.getExtendList(),bw);
            buildGetSet(tableInfo.getFieldInfoList(),bw);
            bw.write("}");
            bw.flush();
        }catch (Exception e){
            LOGGER.error("创建query失败",e);
        }finally {
            if(bw!=null){
                try {
                    bw.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(ow!=null){
                try {
                    ow.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
    private static void buildGetSet(List<FieldInfo> fieldInfoList,BufferedWriter bw) throws Exception{
        for(FieldInfo fieldInfo:fieldInfoList){
            String tempField= StringUtils.processField(fieldInfo.getPropertyName(),true);
            bw.write("\tpublic void set"+tempField+"("+fieldInfo.getJavaType()+" "+fieldInfo.getPropertyName()+")" +" {");
            bw.newLine();
            bw.write("\t\tthis."+fieldInfo.getPropertyName()+" = "+fieldInfo.getPropertyName()+";");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
            bw.newLine();

            bw.write("\tpublic "+fieldInfo.getJavaType()+ " get"+tempField+"("+") " +" {");
            bw.newLine();
            bw.write("\t\treturn this."+fieldInfo.getPropertyName()+";");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
            bw.newLine();
        }
    }
}
